﻿/*----------------------------------------------------------------
// Copyright (C) 2011 上海
// 版权所有。 
//
// 文件名：DataAccessCardRecharge
// 文件功能描述：充值数据访问层
//
// 
// 创建标识：2012-06-22 王冲
//
// 修改标识：
// 修改描述：
//
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DbAdapter.sqldbAdapter;
using System.Data.SqlClient;

namespace DAO.WaiGaoQiao
{
    public class DataAccessCardRecharge
    {
        public DataSet GetInfo(string select)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "select PID,DEPTNAME,EMPNO,姓名,卡内余额 from FILE_PERSONNEL_VIEW where cardserial=" + select;
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                if (ds.Tables[0].Rows.Count == 0)
                {
                    query = "select PID,DEPTNAME,EMPNO,姓名,卡内余额 from FILE_PERSONNEL_VIEW where empno='" + select+"' and 卡状态='正常'";
                    ds = db.ExecuteDataset(conn, CommandType.Text, query);
                }
                return ds;
            }
        }

        public DataSet GetList(int pagesize, int startindex, string strDateTime)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                int startrow = pagesize * (startindex - 1);
                int endrow = pagesize * startindex + 1;
                string query = "select * from (select ROW_NUMBER() OVER(ORDER BY rdatetime DESC) rownumber,* " +
                    "from CardRecharge_VIEW where convert(varchar(10),RDateTime,120)='" + strDateTime + "') B " +
                    "where B.rownumber > " + startrow.ToString() + " and B.rownumber < " + endrow.ToString(); ;
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }

        public int GetListCount(string strDateTime)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "select count(*) from CardRecharge_VIEW where convert(varchar(10),RDateTime,120)='" + strDateTime + "'";
                int count = Convert.ToInt32(db.ExecuteScalar(conn, CommandType.Text, query));
                return count;
            }
        }

        /// <summary>
        /// 保存（给卡增款）
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int SaveIncreaseMoney(object[] obj)
        {
            sqldb db = new sqldb();
            object objBalance;
            int decBalance;
            int count = 0;
            objBalance = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, "select Balance from FundsOperating_View where EmpNo='" + obj[0] + "'");
            decBalance = Convert.ToInt32(objBalance);

            SqlParameter[] parameters = {
                    new SqlParameter("@EMPNO", SqlDbType.VarChar, 20),
                    new SqlParameter("@DEPOSIT_NAME", SqlDbType.VarChar,20),
                    new SqlParameter("@OPERATOR_VALUE",SqlDbType.Int),
                    new SqlParameter("@OLDBALANCE", SqlDbType.Int),
                    new SqlParameter("@NEWBALANCE", SqlDbType.Int),
                    new SqlParameter("@MACNO", SqlDbType.Int),
                    new SqlParameter("@OPERATORID", SqlDbType.VarChar,20),
					new SqlParameter("@RETURN",SqlDbType.VarChar,20)
                    };
            parameters[0].Value = obj[0];
            parameters[1].Value = "现金交费";
            parameters[2].Value = Convert.ToDecimal(obj[1]) * 100;
            parameters[3].Value = decBalance;
            parameters[4].Value = ParameterDirection.Output;
            parameters[5].Value = 0;
            parameters[6].Value = obj[2];
            parameters[7].Direction = ParameterDirection.Output;
            count = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.StoredProcedure, "SP_ADDORDECMONEY", parameters);
            return count;
        }
    }
}
